﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace insidedotnet.UI
{
    /// <summary>
    /// Summary description for Imagem
    /// </summary>
    public class Imagem
    {
        public Imagem() { }

        public Size NewSize(Size Original, Size New)
        {
            Size newSize = new Size(0, 0);
            Decimal originalSize = (((Original.Height * 100) / Original.Width) / 100) + 1;

            //Problemas com retorno 0
            originalSize = originalSize + 1;

            if (originalSize > 1)
            {
                originalSize = ((Original.Width * 100) / Original.Height) / 100;
                newSize.Height = New.Height;
                newSize.Width = (Int32)New.Height * (Int32)originalSize;
            }
            else
            {
                New.Width = New.Width;
                New.Height = (Int32)New.Width * (Int32)originalSize;
            }

            return newSize;
        }
    }

    public static class WaterMark
    {
        public static String Add(String ServerPath, String OriginalFolder, String WaterMarkFolder, String File, String WaterMarkFile)
        {
            String initialPath = ServerPath + "\\" + OriginalFolder + "\\" + File;
            String finalPath = ServerPath + "\\" + WaterMarkFolder + "\\_wm" + File;
            Image image;
            Image finalImage;

            image = Image.FromFile(initialPath);

            finalImage = new Bitmap(image.Width, image.Height, image.PixelFormat);
            Graphics graphic = Graphics.FromImage(finalImage);
            graphic.CompositingQuality = CompositingQuality.HighQuality;
            graphic.SmoothingMode = SmoothingMode.HighQuality;
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

            Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);
            graphic.DrawImage(image, rectangle);

            Image logoImage;
            logoImage = Image.FromFile(ServerPath + "\\" + WaterMarkFolder + "\\" + WaterMarkFile);
            Rectangle rectangleLogo = new Rectangle(0, 0, logoImage.Width, logoImage.Height);

            graphic.DrawImage(logoImage, rectangleLogo);

            finalImage.Save(finalPath);
            image.Dispose();
            finalImage.Dispose();

            return "~//" + WaterMarkFolder + "//_wm" + File;
        }
    }

    public static class Thumbnail
    {
        public static void Create(String ServerPath, String OriginalFolder, String ThumbFolder, String File, int MaxWidth, int MaxHeight)
        {
            try
            {
                //Encriptar os nomes dos arquivos
                String initialPath = ServerPath + "\\" + OriginalFolder + "\\" + File;
                String finalPath = ServerPath + "\\" + ThumbFolder + "\\" + "tb_" + File;

                Image originalImage = Image.FromFile(initialPath);
                Image thumbImage;
                Graphics graphic;

                Imagem _newSize = new Imagem();
                Size finalSize = _newSize.NewSize(originalImage.Size, new Size(MaxWidth, MaxHeight));
                _newSize = null;

                ImageFormat format = originalImage.RawFormat;
                thumbImage = new Bitmap(MaxWidth, MaxHeight);

                graphic = Graphics.FromImage(thumbImage);
                graphic.CompositingQuality = CompositingQuality.HighQuality;
                graphic.SmoothingMode = SmoothingMode.HighQuality;
                graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphic.DrawImage(originalImage, new Rectangle(0, 0, MaxWidth, MaxHeight));

                originalImage.Dispose();
                thumbImage.Save(finalPath, format);
                thumbImage.Dispose();

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
    }
}